<?php

namespace App\Http\Controllers\Admin\Api;

use App\Exceptions\UserException;
use App\Facades\UserService;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Http\Request;
use Sentinel;

class UserController extends Controller
{
    public function login(Request $request)
    {
    	$rules = [
    		'username' => 'required',
    		'password' => 'required|min:6',
    	];
    	$this->validate($request, $rules);

    	UserService::login($request->only(['username', 'password']));
    	return formatJsonResponse();
    }

    public function updatePassword(Request $request)
    {
        $rules = [
            'password' => 'required|min:6',
            'new_password' => 'required|min:6|confirmed'
        ];
        $this->validate($request, $rules);

        UserService::updatePassword($request->password, $request->new_password);
        return formatJsonResponse();
    }

    public function roles()
    {
        return formatJsonResponse(UserService::roles());
    }

    public function add(Request $request)
    {
        $rules = [
            'username' => 'required|unique:users,username|max:12',
            'password' => 'required|confirmed|min:6',
            'name' => 'required',
            'phone' => 'required|telephone',
            'role_id' => 'required|in:2,3,4',
        ];
        $this->validate($request, $rules);

        $user = UserService::hasPermission('superadmin');
        UserService::addAdmin(
            array_merge($request->only(['username', 'password']), ['is_admin' => 1]),
            $request->only(['name', 'phone']),
            $request->role_id,
            $user);
        return formatJsonResponse();
    }

    public function updateAdminInfo(Request $request)
    {
        $rules = [
            'user_id' => 'required|exists:admin_infos,user_id',
            'name' => 'required',
            'phone' => 'required|telephone',
            'role_id' => 'in:2,3,4',
        ];
        $this->validate($request, $rules);

        $user = Sentinel::getUser();
        if(is_null($user)){
            throw new UserException('notFound');
        }
        if($user->hasAccess(['superadmin'])){
            UserService::updateAdminInfo($request->user_id,
                $request->only(['name', 'phone']),
                $request->role_id);
        }else{
            UserService::updateAdminInfo($user->id, $request->only(['name', 'phone']));
        }
        return formatJsonResponse();
    }

    public function deleteAdmin(Request $request)
    {
        $rules = [
            'user_id' => 'required|exists:admin_infos,user_id',
        ];
        $this->validate($request, $rules);

        $user = UserService::hasPermission('superadmin');
        UserService::deleteAdmin($request->user_id, $user);
        return formatJsonResponse();
    }

    public function logout(Request $request)
    {
    	UserService::logout();
    	return formatJsonResponse();
    }

    public function getAdminInfoById(Request $request)
    {
        $rules = [
            'user_id' => 'required|exists:users,id',
        ];
        $this->validate($request, $rules);

        $adminInfo = UserService::findAdminInfoByIdWithUsername($request->user_id);
        return formatJsonResponse([$adminInfo]);
    }

    public function getUserInfoById(Request $request)
    {
        $rules = [
            'user_id' => 'required|exists:users,id',
        ];
        $this->validate($request, $rules);

        $userInfo = UserService::getUserInfoById($request->user_id);
        return formatJsonResponse($userInfo);
    }

    public function process(Request $request)
    {
        $rules = [
            'id' => 'required|exists:after_services,id',
            'status' => 'required|boolean',
        ];
        $this->validate($request, $rules);

        UserService::hasPermission('admin');
        $afterServices = UserService::updateAfterServiceById($request->only(['id']), $request->only(['status']));
        $count = $afterServices->where('status', 0)->count();
        return formatJsonResponse(['count' => $count]);
    }

    public function  updateUserInfo(Request $request)
    {
        $rules = [
            'user_id' => 'required|exists:users,id',
            'name' => 'required',
            'sex' => 'required',
            'phone' => 'required|telephone',
            'installation_address' => 'required',
        ];
        $this->validate($request, $rules);

        UserService::hasPermission('admin');
        UserService::updateUserInfoByUserId($request->user_id, $request->only([
                                                                'name',
                                                                'sex',
                                                                'phone',
                                                                'installation_address',
                                                                ]));
        return formatJsonResponse();
    }

}
